﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ninject.Modules;
using eBook.Core.Contracts;
using eBook.Core.BusinessObjects;
using eBook.Entities;
using AutoMapper;

namespace eBook.EFRepositories
{
  public class RespositoryModule : NinjectModule
  {
    public override void Load()
    {

      Bind<ILibRepository>().To(typeof(LibRepository));
      //Bind<IStudentRepository>().To(typeof(StudentRepository));
      //Bind<IDepartmentRepository>().To(typeof(DepartmentRepository));
      //Bind<IAdaptationService>().To(typeof(AdaptationService));
      //Bind<IUserRepository>().To(typeof(UserRepository));


      Mapper.CreateMap<Author, boAuthor>();
      Mapper.CreateMap<Topic, boTopic>();
      Mapper.CreateMap<Category, boCategory>();
      Mapper.CreateMap<Link, boLink>();
      Mapper.CreateMap<Publisher, boPublisher>();

      Mapper.CreateMap<v_BookInfo, boBookListItem>();
      Mapper.CreateMap<Book, boBook>()
        .ForMember(p => p.PublisherName, option => option.MapFrom(p => p.Publisher.Name))
        .ForMember(p => p.Authors, option => option.MapFrom(p => p.Authors.Select(x => new boAuthor { Name = x.Author.Name }).ToArray()))
        .ForMember(p => p.Categories, option => option.MapFrom(p => p.Categories.Select(x => new boCategory { Name = x.Category.Name }).ToArray()))
        .ForMember(p => p.Topics, option => option.MapFrom(p => p.Topics.Select(x => new boTopic { Name = x.Topic.Name }).ToArray()));

    }

    private int? StringToNullableInt(string value)
    {
      int number = 0;
      if ( int.TryParse(value, out number) )
        return number;

      return new int?();
    }

  }
}
